package com.ruangong.GitClient.app.Password;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.ruangong.GitClient.R;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;


public class PasswordDetailActivity extends AppCompatActivity {

    private TextView textViewUsernameValue;
    private TextView textViewPasswordValue;
    private TextView textViewWebsiteValue;
    private Button deleteButton;
    private Password password;
    private PasswordDBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_passworddetail);

        // 初始化 TextView 和 Button
        textViewUsernameValue = findViewById(R.id.textViewUsernameValue);
        textViewPasswordValue = findViewById(R.id.textViewPasswordValue);
        textViewWebsiteValue = findViewById(R.id.textViewWebsiteValue);
        ImageButton copyUsernameButton = findViewById(R.id.buttonCopyUsername);
        ImageButton copyPasswordButton = findViewById(R.id.buttonCopyPassword);
        deleteButton = findViewById(R.id.buttonDelete);

        // 从 Intent 中获取传递的密码对象
        password = (Password) getIntent().getSerializableExtra("PASSWORD_DETAIL");

        // 如果密码对象有效，则显示在界面上
        if (password != null) {
            // 将密码信息显示在相应的 TextView 中
            textViewUsernameValue.setText(password.getUsername());
            textViewPasswordValue.setText(password.getPassword());
            textViewWebsiteValue.setText(password.getWebsite());
        }

        // 初始化数据库辅助类
        dbHelper = new PasswordDBHelper(this);

        // 设置复制账号按钮的点击事件监听器
        copyUsernameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 复制账号到剪贴板
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("username", password.getUsername());
                clipboard.setPrimaryClip(clip);

                // 提示复制成功
                Toast.makeText(PasswordDetailActivity.this, "账号已复制到剪贴板", Toast.LENGTH_SHORT).show();
            }
        });

        // 设置复制密码按钮的点击事件监听器
        copyPasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 复制密码到剪贴板
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("password", password.getPassword());
                clipboard.setPrimaryClip(clip);

                // 提示复制成功
                Toast.makeText(PasswordDetailActivity.this, "密码已复制到剪贴板", Toast.LENGTH_SHORT).show();
            }
        });


        // 设置删除按钮的点击事件监听器
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletePassword();
            }
        });
    }

    // 删除密码记录的方法
    private void deletePassword() {
        // 获取要删除密码的ID
        int passwordId = password.getId();

        // 获取数据库实例
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // 删除密码记录
        db.delete(PasswordDBHelper.TABLE_NAME, PasswordDBHelper.COLUMN_ID + " = ?",
                new String[]{String.valueOf(passwordId)});

        // 关闭数据库连接
        db.close();

        // 返回到上一个活动
        finish();
    }

}



